package com.freesky.provider.order.controller;

import com.freesky.common.dao.entity.Order;
import com.freesky.common.vo.OrderInfo;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.common.vo.VOOrder;
import com.freesky.common.dao.entity.Thanks;
import com.freesky.provider.order.service.IOrderService;
import com.freesky.provider.order.vo.VOOrderDetail;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author Freesky
 */
@RestController
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @GetMapping(value = "/order/list")
    public DataTablesResult getOrderList(
            @RequestParam(value = "draw") int draw,
            @RequestParam(value = "start") int start,
            @RequestParam(value = "length") int length,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir) {

        //获取客户端需要排序的列
        String[] cols = {"checkbox", "order_id", "payment", "shipping_code", "user_id", "buyer_nick", "create_time", "update_time", "payment_time", "close_time", "end_time", "status"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if (orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if (orderDir == null) {
            orderDir = "desc";
        }
        DataTablesResult result = orderService.getOrderList(draw, start, length, search, orderColumn, orderDir);
        return result;
    }

    @GetMapping(value = "/order/count")
    public JSONResponse getOrderCount() {

        Long result = orderService.countOrder();
        return JSONResponseDiretor.buildSuccessJSONResponse(result);
    }

    @GetMapping(value = "/order/detail/{orderId}")
    public JSONResponse getOrderDetail(@PathVariable(value = "orderId") String orderId) {

        VOOrderDetail VOOrderDetail = orderService.getOrderDetail(orderId);
        return JSONResponseDiretor.buildSuccessJSONResponse(VOOrderDetail);
    }

    @PostMapping(value = "/order/remark")
    public JSONResponse remark(
            @RequestParam(value = "orderId") String orderId,
            @RequestParam(value = "message", required = false) String message) {
        return orderService.remark(orderId, message);
    }

    @PostMapping(value = "/order/deliver")
    public JSONResponse deliver(
            @RequestParam(value = "orderId") String orderId,
            @RequestParam(value = "shippingName") String shippingName,
            @RequestParam(value = "shippingCode") String shippingCode,
            @RequestParam(value = "postFee") BigDecimal postFee) {

        return orderService.deliver(orderId, shippingName, shippingCode, postFee);
    }

    @GetMapping(value = "/order/cancel/{orderId}")
    public JSONResponse cancelOrderByAdmin(@PathVariable(value = "orderId") String orderId) {

        return orderService.cancelOrderByAdmin(orderId);
    }

    @DeleteMapping(value = "/order/del/{ids}")
    public JSONResponse delete(@PathVariable(value = "ids") String[] ids) {

        for (String id : ids) {
            orderService.deleteOrder(id);
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }




    @GetMapping(value = "/order/member/list")
    public JSONResponse getMemberOrderList(
            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "5") int size) {

        return orderService.getMemberOrderList(Long.valueOf(userId), page, size);
    }


    @GetMapping(value = "/order/detail")
    public JSONResponse getOrderById(@RequestParam(value = "orderId") String orderId){

        return orderService.getOrderById(orderId);
    }
    @GetMapping(value = "/order/member/detail")
    public JSONResponse getOrder(String orderId){

        return orderService.getOrder(Long.valueOf(orderId));
    }

    @PostMapping(value = "/order/member/add")
    public JSONResponse addOrder(@RequestBody OrderInfo orderInfo){

        return orderService.createOrder(orderInfo);
    }

    @PostMapping(value = "/order/member/cancel")
    public JSONResponse cancelOrder(@RequestBody VOOrder order){

        return orderService.cancelOrder(order.getOrderId());
    }

    @GetMapping(value = "/order/member/del")
    public JSONResponse delOrder(String orderId){
        return orderService.delOrder(Long.valueOf(orderId));
    }

    @PostMapping(value = "/order/member/pay")
    public JSONResponse payOrder(@RequestBody Order order){
        return orderService.payOrder(order);
    }

    @PostMapping(value = "/order/alipay/notify")
    public String alipayNotify() {
        return orderService.alipayNotify();
    }


}
